// pages/msg/msg.js
import config from "../../config.js";
const base = config;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //个人中心全局
    mine: {
      backgroundImg: "../../image/mine-background.png",
      name: "陈生珠",
      carNo: "",
      brand: "",
      level: "",
      member_no: "**** **** **** 4673",
      score: "30",
      coupon: "6",
      orders: "3",
      exper: "工作5年，服务210位客户"
    },
    // 我的管家
    manager: {
      header: "../../image/personal.png",
      name: "",
      carNo: "",
      level: 4.5,
      exper: "工作5年，服务210位客户",
    },

    // 消息列表
    msg: [{
        url: "./mineinfo/mineinfo",
        icon: "../../image/personal.png",
        title: "个人信息",
        time: "12:30",
      },
      {
        url: "../member/activation",
        icon: "../../image/vip.png",
        title: "菇咚会员",
        time: "12:30",
      },
      {
        url: "../changePwd/changePwd",
        icon: "../../image/changepwd.png",
        title: "修改密码",
        time: "12:30",
      }, {
        url: "./carList/carList",
        icon: "../../image/cars.png",
        title: "车辆信息",
        time: "12:30",
      }, {
        url: "../peccancy/step2",
        icon: "../../image/query.png",
        title: "违章查询",
        time: "12:30",
      }, {
        url: "./feedback/feedback",
        icon: "../../image/feedback.png",
        title: "意见反馈",
        time: "12:30",
      },
      {
        url: "../../pages/about/about",
        icon: "../../image/about.png",
        title: "关于我们",
        time: "12:30",
      }
    ],
    //个人信息
    user: {},
    membershipcard: {}
  },

  cardDetail: function() {
    wx.navigateTo({
      url: './cardDetail/cardDetail',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var showRight = wx.getStorageSync("showRight");
    if (showRight) {
      wx.navigateTo({
        url: '../member/memberItem',
        success: function() {
          wx.setStorageSync("showRight", null);
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getUser();
    // if (!user) {
    //   app.globalData.getUserInfo();
    // }
    var userInfo = wx.getStorageSync("user");
    this.setData({
      user: userInfo
    })
  },
  goItme: function() {
    wx.navigateTo({
      url: '../member/memberItem',
    })
  },
  goOrders: function() {
    wx.setStorageSync("mineOrders", 1);
    wx.switchTab({
      url: '../order/order',
    })
  },
  goScore: function() {
    wx.navigateTo({
      url: '../userscore/userscore',
    })
  },
  getUser: function() {
    var that = this;
    wx.showLoading({
      title: '获取用户信息...',
    })
    var sessionId = wx.getStorageSync("session");
    wx.request({
      url: base.getUserInfo,
      header: {
        'content-type': 'application/json',
        'Cookie': sessionId
      },
      success: function(res) {
        console.log(res.data);
        wx.hideLoading();
        if (res.data.status == 200) {
          //保存userInfo;
          var userInfo = res.data.data;
          if (res.data.card != null) {
            that.setData({
              membershipcard: res.data.card
            })
          }
          wx.setStorageSync("user", userInfo);
          that.setData({
            user: userInfo
          })
          console.log(that.data.user);
        }
      },
      fail: function() {}
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.setStorageSync("showRight", null);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  loginOut: function() {
    wx.showToast({
      title: '注销中...',
      icon: "loading",
      mask: true
    })
    wx.request({
      url: base.loginOut,
      header: {
        'Cookie': wx.getStorageSync("session")
      },
      success: function() {
        wx.clearStorage();
        wx.setStorageSync('user', null);
        wx.setStorageSync('isLogin', false);
        wx.setStorageSync('userType', null);
        wx.setStorageSync('openID', null);
        wx.setStorageSync('session', null);
        wx.reLaunch({
          url: '../login/login',
        })

        wx.showToast({
          title: '退出成功！',
          icon: "loading",
          mask: true,
          duration: 1000
        })
      }
    })
  },
  goToActive: function() {
    wx.navigateTo({
      url: "../member/activation",
    })
  }
})